﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fiddler;
using System.Xml;
using System.Windows.Forms;

namespace iFiddlerX
{
    /// <summary>
    /// 自动规则修改类
    /// </summary>
    public class AutoRuleHelper
    {
        string Match;
        string Replace;
        public delegate void Delgate_AutoRule(Session oSession, ListView Lv);
        /// <summary>
        /// 自动修改规则
        /// </summary>
        /// <param name="oSession"></param>
        /// <param name="Lv"></param>
        public void AutoRule(Session oSession,ListView Lv)
        {
            if (!Lv.InvokeRequired)
            {
                foreach (ListViewItem lvi in Lv.Items)//遍历Listview中的所有item
                {
                    if (lvi.Checked)//如果Checkbox选中
                    {
                        switch (lvi.SubItems[1].Text)//判断Type类型来进行相应修改
                        {
                            case "发送数据":
                                Match = lvi.SubItems[2].Text;
                                Replace = lvi.SubItems[3].Text;
                                oSession.utilReplaceInRequest(Match, Replace);
                                break;
                            case "返回数据":
                                Match = lvi.SubItems[2].Text;
                                Replace = lvi.SubItems[3].Text;
                                oSession.utilReplaceInResponse(Match, Replace);
                                break;
                            case "网址":
                                Match = lvi.SubItems[2].Text;
                                Replace = lvi.SubItems[3].Text;
                                oSession.fullUrl=oSession.fullUrl.Replace(Match, Replace);
                                break;
                            case "COOKIE":
                                Match = lvi.SubItems[2].Text;
                                Replace = lvi.SubItems[3].Text;
                                oSession.oRequest.headers["Cookie"] = oSession.oRequest.headers["Cookie"].Replace(Match, Replace);
                                break;
                            case "REFERER":
                                Match = lvi.SubItems[2].Text;
                                Replace = lvi.SubItems[3].Text;
                                oSession.oRequest.headers["Referer"] = oSession.oRequest.headers["Referer"].Replace(Match, Replace);
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
            else
            {
                Delgate_AutoRule delgate_autorule = new Delgate_AutoRule(AutoRule);
                Lv.Invoke(delgate_autorule, oSession, Lv);
            }
        }
    }
}
